Page({
  data: {
    courseId: null,
    courseInfo: {
      title: '',
      desc: '',
      teacher: '',
      totalLessons: 0,
      completedLessons: 0,
      lessons: []
    },
    currentLesson: null,
    animationData: {},
    showLessonTip: false,
    currentTipText: ''
  },

  onLoad(options) {
    const { id } = options;
    this.setData({ courseId: id });
    this.getCourseDetail(id);
  },

  getCourseDetail(id) {
    // 模拟获取课程详情数据
    const courseInfo = {
      title: '英语音标入门课程',
      desc: '从零开始学习英语音标，掌握准确发音',
      teacher: '王老师',
      totalLessons: 10,
      completedLessons: 0,
      lessons: [
        {
          id: 1,
          title: '元音音标介绍',
          duration: '15:00',
          status: 'locked', // locked, available, completed
          videoUrl: ''
        },
        {
          id: 2,
          title: '辅音音标介绍',
          duration: '20:00',
          status: 'locked',
          videoUrl: ''
        }
      ]
    };

    this.setData({ courseInfo });
  },

  startLesson(e) {
    const { id } = e.currentTarget.dataset;
    const lesson = this.data.courseInfo.lessons.find(l => l.id === id);
    
    if (lesson.status === 'locked') {
      wx.showToast({
        title: '请先完成前面的课程',
        icon: 'none'
      });
      return;
    }

    this.setData({ currentLesson: lesson });
    // 跳转到视频播放页面
    wx.navigateTo({
      url: `/pages/video-player/video-player?lessonId=${id}&courseId=${this.data.courseId}`
    });
  },

  // 更新课程进度
  updateProgress() {
    const { courseInfo } = this.data;
    const completedCount = courseInfo.lessons.filter(l => l.status === 'completed').length;
    
    this.setData({
      'courseInfo.completedLessons': completedCount
    });

    // 保存进度
    wx.setStorageSync(`course_progress_${this.data.courseId}`, {
      completedLessons: completedCount,
      totalLessons: courseInfo.totalLessons,
      lastStudyTime: new Date().getTime()
    });
  },

  // 格式化进度条样式
  formatProgressStyle(completed, total) {
    if (!total) return 'width:0%';
    const progress = Math.min((completed / total * 100), 100);
    return `width:${progress}%`;
  },

  // 点击课程时的动画效果
  onLessonTap(e) {
    const { id } = e.currentTarget.dataset;
    const lesson = this.data.courseInfo.lessons.find(l => l.id === id);
    
    // 创建动画实例
    const animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease'
    });

    // 点击时的缩放动画
    animation.scale(0.95).step();
    animation.scale(1).step();
    
    this.setData({
      animationData: animation.export()
    });

    // 根据课程状态显示不同提示
    if (lesson.status === 'locked') {
      this.showTip('需要完成前面的课程才能解锁哦~');
      return;
    }

    this.startLesson(e);
  },

  // 显示提示信息
  showTip(text) {
    this.setData({
      showLessonTip: true,
      currentTipText: text
    });

    setTimeout(() => {
      this.setData({
        showLessonTip: false
      });
    }, 2000);
  },

  // 预览课程视频
  previewVideo(e) {
    const { id } = e.currentTarget.dataset;
    const lesson = this.data.courseInfo.lessons.find(l => l.id === id);
    
    if (lesson.status === 'locked') return;

    wx.showLoading({
      title: '加载预览...'
    });

    // 模拟加载延迟
    setTimeout(() => {
      wx.hideLoading();
      // 这里可以添加视频预览逻辑
      wx.showToast({
        title: '暂无预览视频',
        icon: 'none'
      });
    }, 1000);
  }
}); 